<?php
namespace Kuoqi;
class Curl{
	
	public $isCustom = false;//是否自定义请求内容模块
	
	public $curl = '';//请求地址信息
	
	public $server_language = 'php';//服务端语言【php,java】
	
	public $method = 'GET';//请求方式
	
	public $headinfo = array('Content-Type:multipart/form-data');
	
	public $params = array();//参数信息
	
	public $useagent = '';//用户请求头信息
	
	public $cookie = '';//判定是否有cookie，如果有的话，将cookie传入
	private $cookie_tmp = '';//用户中间存储cookie的信息
	
	
	//超时类型[S/MS]
	public $timeoutType = 'S';
	//超时时间
	public $timeout = 0;
	
	//是否验证https
	public $VerifyHost = false;
	public $VerifyPeer = false;
	
	public $responseHead = null;//相应heade
	
	public $responseStream = null;//相应返回流
	
	public $response = null;//相应返回数据具体信息
	public $request = null;//请求包相关信息
	
	//响应头类型
	public $responseType = '';
	
	public $resource = NULL;//资源信息
	
	//CURLOPT_INTERFACE
	
	final function request(){
		$this->resource = curl_init();
		$this->request = new Curl\Request($this);
		curl_setopt($this->resource,CURLOPT_URL,$this->curl); 
		curl_setopt($this->resource,CURLOPT_CUSTOMREQUEST,$this->method);
		
		curl_setopt($this->resource,CURLOPT_SSL_VERIFYPEER,$this->VerifyHost);
		curl_setopt($this->resource,CURLOPT_SSL_VERIFYHOST,$this->VerifyPeer);
		curl_setopt($this->resource,CURLOPT_RETURNTRANSFER,true);
		
		
		//发送头部信息
		curl_setopt($this->resource,CURLOPT_HTTPHEADER,$this->headinfo);
		
		//使用版本信息
		$this->useagent
			&&(curl_setopt($this->resource, CURLOPT_USERAGENT,$this->useagent));
		
		if($this->cookie){
			//存储cookies
			//设定HTTP请求中"Cookie: "部分的内容。多个cookie用分号分隔，分号后带一个空格(例如， "fruit=apple; colour=red")。 
			//发送cookie
			if($this->cookie_tmp){
				$this->cookie = $cookie_tmp;
			}
			curl_setopt($this->resource,CURLOPT_COOKIE,$this->cookie);
			curl_setopt($this->resource,CURLOPT_COOKIEJAR,$this->cookie_tmp);
		}
		
		$this->response = new Curl\Response($this);
		curl_close($this->resource); 
	}
	
	
	
	//打开请求，也就是初始化打开操作
	//其实在这里为本地初始化数据等等，基础的信息
	public function open(){
		//打开资源流
		$this->resource = curl_init();
		//初始化请求的部分
		$this->request = new Curl\Request($this);
	}
	
	//发送数据信息
	//打开之后发送很多很多的数据，
	//实际w为发送数据的那部分
	public function send(){
		curl_setopt($this->resource,CURLOPT_URL,$this->curl); 
		curl_setopt($this->resource,CURLOPT_CUSTOMREQUEST,$this->method);
		
		curl_setopt($this->resource,CURLOPT_SSL_VERIFYPEER,$this->VerifyHost);
		curl_setopt($this->resource,CURLOPT_SSL_VERIFYHOST,$this->VerifyPeer);
		curl_setopt($this->resource,CURLOPT_RETURNTRANSFER,true);
		
		
		//发送头部信息
		curl_setopt($this->resource,CURLOPT_HTTPHEADER,$this->headinfo);
		
		//使用版本信息
		$this->useagent
			&&(curl_setopt($this->resource, CURLOPT_USERAGENT,$this->useagent));
		
		$this->response = new Curl\Response($this);
		curl_close($this->resource); 
	}
	
	static function sendTo($url = '', $method = '', $params = array(), $rsptype = '',$timeout = 0){
		$app = new self();
		$url&&$app->curl = $url;
		$method&&$app->method = strtoupper($method);
		$params&&$app->params = $params;
		$rsptype&&$app->responseType = strtoupper($rsptype);
		$timeout&&$app->TimeOut = floatval($timeout);
		$app->request();
		return $app->response->output($app->responseType);
	}
	
	//$curl->setDatas();//设置多参数
	final function setParams($data = array()){
		$this->params = $data;	
	}
	
	//
	final function getParams(){
		return $this->params;	
	}
	
	//$curl->setMethod();//配置请求方式
	final function setMethod($method){
		$this->method = strtoupper($method);	
	}
	
	final function getMethod(){
		return $this->method;	
	}
	
	
	final function setDatatype($datatype){
		$this->responseType = strtoupper($datatype);	
	}
	
	//$curl->setAction();//设置请求地址
	final function setCurl($curl){
		$this->curl = $curl;
	}
	
	//$curl->setTimeout();//设置超时时间
	final function setTimeout($code,$isMS = true){
		$this->timeout = intval($code);
		if($isMS){
			$this->timeoutType = 'MS';	
		}else{
			$this->timeoutType = 'S';	
		}
	}
	
	//$curl->setSSl();//配置秘钥信息
	final function setSSL(){}
	
	//$curl->setProxy();//配置代理信息
	final function setProxy(){}
}
